<?php

define("USERTYPE_OWN", 1);
define("USERTYPE_YAHOO", 2);
define("USERSTATUS_CHECK", 1);
define("USERSTATUS_USE", 2);
define("USERSTATUS_DELETE", 3);

/*
require_once '../config/conf.inc.php';
$ret = AccountManager::send_register_mail("mayan", "350932338@qq.com", "23234234", $err);
echo "ret=$ret\n";
echo "err=$err\n";
*/

Class AccountManager
{
	static function check_yahoo_mail($username, $pass)
	{
		$url = 'http://edit.bjs.yahoo.com'; 
		$fields = array( 
		'hao_mail_username'=>'' , 
		'hao_mail_passwd'=>'' , 
		'login'=>'mayan_1104@yahoo.com.cn' , 
		'passwd'=>'30728' , 
		'domainss'=>'yahoo' , 
		'.intl'=>'cn' , 
		'.src'=>'ym' 
		); 

		foreach($fields as $key=>$value)	{ $fields_string .= $key.'='.$value.'&' ; } 
		$fields_string = rtrim($fields_string ,'&') ; 

		$ch = curl_init() ; 
		curl_setopt($ch, CURLOPT_URL,$url) ; 
		curl_setopt($ch, CURLOPT_REFERER, "http://hao123.com/"); 
		curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; WPS; QQDownload 627; .NET CLR 2.0.50727)'); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string); 

		$result = curl_exec($ch) ; 
		curl_close($ch);
		
		if (!$result)	return false;
	
		$pos = strpos($result, "\n\n");
		$header = substr($result, $pos);
		$cnt = substr_count($header, 'Set-Cookie');
		
		if ($cnt >=1)
			return	true;
		else
			return false;
	}
	
	static function send_register_mail($username, $email_address, $reg_code, &$err)
	{
		$err = "";
		if (empty($username) || empty($email_address) || empty($reg_code))
		{
			$err = "params_empty";
			return false;
		}
		
		date_default_timezone_set('PRC');
		$mail = new PHPMailer();
		$mail->IsSMTP();
		$mail->Host = "smtp.qq.com";	
		$mail->Port = 25;
		$mail->SMTPAuth = true;
		$mail->CharSet = "UTF-8";
		$mail->Username = "350932338";
		$mail->Password = "";			
		$mail->From = "350932338@qq.com";
		$mail->FromName = "泥团网";
		$mail->AddAddress($email_address, $username);
		$mail->AddReplyTo("horstonema@iote.cc", "horstonema");
		$mail->WordWrap = 50;
		/** 
		$mail->AddAttachment("/var/tmp/file.tar.gz");		
		$mail->AddAttachment("/tmp/image.jpg", "new.jpg");	
		*/
		$mail->IsHTML(true);
		$mail->Subject = "泥团注册";			
		$mail->Body = "测试邮件  http://ntuan.com/register_mail.php?email=".urlencode($email_address);	
		$mail->Body.= "&register_code=".urlencode($reg_code)."\n";
		$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
		
		if(!$mail->Send())
		{
		   $err = $mail->ErrorInfo;
		   return false;
		}

		return true;
	}
	
	static function get_register_code($username, $email)
	{
		$username = trim($username);
		$pass = trim($pass);
		$reg_code = md5($username."/".$email);
		
		return $reg_code;
	}
	
	static function get_email($user)
	{
		$err = "";	
		if (empty($user))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FEmail from Tbl_UserAccount where FAccount='$user' or FEmail='$user'";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "query_err";
			return false;
		}
		
		$res = $db->fetch_array();
		$email = $res['FEmail'];
		if (!empty($email))
		{
			return  $email;	
		}
		
		return false;
	}
	
	static function modify_email($account, $new_email, &$err)
	{
		$err = "";
		if (empty($account) || empty($new_email))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_UserAccount set FEmail='$new_email' where FAccount='$account' or FEmail='$account' limit 1";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function check_email_exist($email)
	{
		$err = "";	
		if (empty($email))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FEmail from Tbl_UserAccount where FEmail='$email'";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "query_err";
			return false;
		}
		
		$res = $db->fetch_array();
		$email = $res['FEmail'];
		if (!empty($email))
		{
			return  true;	
		}
		
		return false;
	}
	
	static function modify_password($account, $new_pass, $err)
	{
		$err = "";
		if (empty($account) || empty($new_pass))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_UserAccount set FPasswd=password('$new_pass') where FAccount='$account' or FEmail='$account' limit 1";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function check_register_mail($email_address, $reg_code, &$err)
	{
		$err = "";
		if (empty($email_address) || empty($reg_code))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
	
		$sql =  "select FUId, FAccount from Tbl_UserAccount where FEmail='$email_address'";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "select_err";
			return false;
		}
		
		$res = $db->fetch_array();
		$user = $res['FAccount'];
		$uid = $res['FUId'];
		if (empty($uid))	return  false;
		
		$code = AccountManager::get_register_code($user, $email_address);
		if ($reg_code != $code)	return false;
		
		$ret = AccountManager::set_user_status($uid, USERSTATUS_USE, $err);
		return $ret;
	}
	
	static function set_user_status($uid, $status, &$err)
	{
		$err = "";
		if (empty($uid))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_UserAccount set FStatus=$status where FUId=$uid";
		$db->query($sql);
		$ret = $db->db_affected_rows();
		if ($ret != 1)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function check_user_account($user, $email, &$err)
	{
		$err = "";	
		if (empty($user) || empty($email))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FUId from Tbl_UserAccount where FAccount='$user' or FEmail='$email'";
		$db->query($sql);
		if ($db->is_error)
		{
			return false;
		}
		
		$res = $db->fetch_array();
		$uid = $res['FUId'];
		if (!empty($uid))
		{
			return  true;	
		}
		
		return false;
	}
	
	static function check_login($user, $pass, $type, &$err)
	{
		$err = "";
		if (empty($user) || empty($pass) || empty($type))
		{
			$err = "params_empty";
			return false;
		}
		
		if ($type == USERTYPE_OWN)
		{
			return AccountManager::check_login_own($user, $pass, $err);
		}
		
		return false;
	}
	
	static function check_login_own($user, $pass, &$err)
	{
		$err = "";
		if (empty($user) || empty($pass))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FUId,FStatus from Tbl_UserAccount where FAccount='$user' and FPasswd=password('$pass')";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "query_err";
			return false;
		}
		
		$res = $db->fetch_array();
		$uid = $res['FUId'];
		$status = $res['FStatus'];
		if (empty($uid))	return false;
		
		return $status;
	}
	
	static function insert_user_account($user, $pass, $email, $type, &$err)
	{
		$err = "";
			
		if (empty($user) || empty($pass) || empty($email) || empty($type))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "insert into Tbl_UserAccount set FAccount='$user', FPasswd=password('$pass'), ".
			"FEmail='$email', FType=$type, FStatus=".USERSTATUS_CHECK;
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "insert_err";
			return false;
		}
		
		return true;
	}
};

?>
